package grafica;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import javax.swing.JPanel;

import sprite.Giocatore;


public class NextLevelPanel extends JPanel implements Runnable {
	private MediaTracker media;
	private Toolkit tool;
	private Myframe mf;
	private int livelloPartenza;
	private int yPartenza;
	private int yArrivo;
	private int xPartenza;
	private int xArrivo;
	private boolean stopThread;
	private boolean type;
	private Giocatore p;
	
	public NextLevelPanel(int livelloPartenza,Myframe mf,Giocatore p,boolean type){
		super();
		this.type = type;
		this.p = p;
		this.mf = mf;
		this.livelloPartenza = livelloPartenza;
		calcolaPercorso();
		stopThread = false;
		media = new MediaTracker(this);
		tool = Toolkit.getDefaultToolkit();
		media.addImage(tool.getImage("images/Menu/calabria.png"), 0);
		media.addImage(tool.getImage("images/Menu/testaLivello.gif"), 1);
		media.addImage(tool.getImage("images/Menu/spazio1.gif"), 2);
		try {
			media.waitForAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.setFocusable(true);
		new Thread(this).start();
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		//g.setColor(Color.BLUE);
		//g.fillRect(0, 0, Myframe.SCREEN_WIDTH, Myframe.SCREEN_HEIGHT);
		g.drawImage(tool.getImage("images/Menu/spazio1.gif"), 0, 0, null);
		g.drawImage(tool.getImage("images/Menu/calabria.png"), 200, 0, null);
		g.drawImage(tool.getImage("images/Menu/testaLivello.gif"), xPartenza, yPartenza, null);

		
	}
	
	private void calcolaPercorso(){
		switch(livelloPartenza){
		case 0:
			xPartenza = 270;
			xArrivo = 270;
			yPartenza = 545;
			yArrivo = 445;
			break;
		case 1:
			xPartenza = 270;
			xArrivo = 370;
			yPartenza = 445;
			yArrivo = 445;
			break;
		case 2:
			xPartenza = 370;
			xArrivo = 370;
			yPartenza = 445;
			yArrivo = 283;
			break;
		case 3:
			xPartenza = 370;
			xArrivo = 472;
			yPartenza = 283;
			yArrivo = 220;
			break;
		case 4:
			xPartenza = 472;
			xArrivo = 355;
			yPartenza = 220;
			yArrivo = 220;
			break;
		}
	}

	public void run() {
		while(!stopThread){
			if (yPartenza == yArrivo){
				if(xPartenza < xArrivo){
					xPartenza++;
				}
				else if(xPartenza > xArrivo){
					xPartenza--;
				} 
			}
			else if (xPartenza == xArrivo){
				if(yPartenza > yArrivo){
					yPartenza--;
				}
			}
			else{
				if(xPartenza < xArrivo){
					xPartenza++;
				}
				else if(yPartenza > yArrivo){
					yPartenza--;
				}
			}
			if (yPartenza == yArrivo && xPartenza == xArrivo){
				stopThread = true;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(livelloPartenza == 0) {
					this.mf.avviaPartita(1, type);
				} else {
					this.mf.changePanel(p, livelloPartenza+1,true, type);
				}
				//this.mf.getPannelloPrincipale().removeAll();
				//this.mf.getPannelloPrincipale().updateUI();
			}
			System.out.println("xPartenza --> "+xPartenza);
			System.out.println("xArrivo --> "+xArrivo);
			System.out.println("yPartenza --> "+yPartenza);
			System.out.println("yArrivo --> "+yArrivo);
			System.out.println();
			this.repaint();
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public boolean isStopThread() {
		return stopThread;
	}

	public void setStopThread(boolean stopThread) {
		this.stopThread = stopThread;
	}

}
